package com.haochang.chunkvod.controller.activity.hostess;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.app.utils.VodDialog;
import com.haochang.chunkvod.controller.adapter.hostess.SortRoomAreaAdapter;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.log.Logger;
import com.haochang.chunkvod.view.vod.listView.DragSwapListView;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Yangjie
 *         className ReserveActivity
 *         created at  2016/8/23  18:08
 *         咨客 开房排序 区域层排序
 */
public class SortRoomFirstActivity extends BaseActivity
{
	private TitleView titleView;
	private BaseTextView tv_area;//1区，2区
	private DragSwapListView lv_sort_room;//拖动换位listview
	private BaseTextView tv_sort;

	private SortRoomAreaAdapter adapter;
	private List<HostessInfo> listRoomInfo = new ArrayList<HostessInfo>();//保存原始数据
	private int sortType;//排序类型，0默认不能拖动排序(可置顶，置底)，1激活排序功能
	private HostessData hostessData;
	private JSONArray jsonArray;//原始数据
	private JSONArray jsonArrayChange = new JSONArray();//排序变化后的数据
	private boolean isFirst = true;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResume()
	{
		super.onResume();
		getSortRoomAreaList(isFirst);
		isFirst = false;
	}

	@Override
	public void receiveParam()
	{
	}

	@Override
	public void initData()
	{
		hostessData = new HostessData(SortRoomFirstActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_sort_room_area);
		tv_area = (BaseTextView) findViewById(R.id.tv_area);
		tv_sort = (BaseTextView) findViewById(R.id.tv_sort);
		lv_sort_room = (DragSwapListView) findViewById(R.id.lv_sort_room);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(R.string.sort_room).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				if (adapter != null && adapter.hasDataChanged())
				{
					showPopSave();
				} else
				{
					finish();
					animRightToLeft();
				}
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("回到首页");
				list.add("修改密码");
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(SortRoomFirstActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							backToHome();
							break;
						case 1:
							goToPasswordModify();
							break;
						case 2:
							loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
	}

	@Override
	public void initViewsData()
	{
		tv_area.setText("区域");
		tv_sort.setOnClickListener(new OnBaseClickListener()
		{
			@Override
			public void onBaseClick(View v)
			{
				if (sortType == 0)
				{
					//未获取数据时直接返回 顺便再次请求列表
					if (listRoomInfo == null || listRoomInfo.size() <= 0)
					{
						getSortRoomAreaList(true);
						return;
					}
					sortType = 1;
					//正常--排序
					//启动排序
					tv_sort.setText(R.string.complete);
					lv_sort_room.setCanDrag(true);
					for (HostessInfo info : listRoomInfo)
					{
						info.setShowBottom(false);
						info.setShowRightDrawable(true);
					}
					adapter.notifyDataSetChanged();
				} else if (sortType == 1)
				{
					//未获取数据时直接返回 顺便再次请求列表
					if (listRoomInfo == null || listRoomInfo.size() <= 0)
					{
						getSortRoomAreaList(true);
						return;
					}
					//正常--排序--完成
					//排序完成
					//请求保存开房排序
					requestSaveSortRoomArea();
				}
			}
		});
	}

	/**
	 * created at  2016/8/25  17:18
	 * 保存 开房排序
	 */
	private void requestSaveSortRoomArea()
	{
		final JSONArray jsonArrayTranslation = new JSONArray();
		getJSONArray(jsonArrayTranslation);
		hostessData.requestSortRoomSave(jsonArrayTranslation, false, new HostessData.ResultResponse()
		{
			@Override
			public void onRequestSuccess()
			{
				getJSONArray(jsonArrayChange);
				ToastUtils.showText(SortRoomFirstActivity.this, "保存开房顺序成功");
				sortType = 0;
				tv_sort.setText(R.string.sort);
				lv_sort_room.setCanDrag(false);
				for (HostessInfo info : listRoomInfo)
				{
					info.setShowBottom(false);
					info.setShowRightDrawable(false);
				}
				List<HostessInfo> dataList = adapter.getDataList();
				List<HostessInfo> dataListNew = new ArrayList<HostessInfo>();
				if (dataList != null && dataList.size() > 0)
				{
					dataListNew.addAll(dataList);
				}
				adapter.listTemp.clear();
				adapter.listTemp.addAll(dataListNew);
				listRoomInfo.clear();
				listRoomInfo.addAll(dataListNew);
				jsonArray = jsonArrayChange;
				adapter.arrayAfter = jsonArrayChange;
				adapter.notifyDataSetChanged();
			}

			@Override
			public void onFail(int errorNo, String errorMsg)
			{
				ToastUtils.showText(SortRoomFirstActivity.this, "保存开房顺序失败");
			}
		});
	}

	/**
	 * 将传入的JSONArray
	 * @param arrayAdd  变化后的JSONArray
	 */
	private void getJSONArray(JSONArray arrayAdd)
	{
		if (jsonArray != null)
		{
			if (listRoomInfo != null && listRoomInfo.size() > 0)
			{
				int lenth = listRoomInfo.size();
				for (int k = 0; k < lenth; k++)
				{
					int length = jsonArray.length();
					for (int i = 0; i < length; i++)
					{
						JSONObject jsonObject1 = jsonArray.optJSONObject(i);
						try
						{
							if ((listRoomInfo.get(k)).getAreaId().equals(jsonObject1.optString("area_id")))
							{
								arrayAdd.put(k, jsonObject1);
								break;
							}
						} catch (JSONException e)
						{
							Logger.e("getJSONArray:" + e.getMessage());
						}
					}
				}
			}
		}
	}

	/**
	 * 获取分区列表
	 *
	 * @param isLoading 是否显示网络加载的loading
	 */
	public void getSortRoomAreaList(final boolean isLoading)
	{
		hostessData.requestSortRoomAreaList(isLoading, new HostessData.ResultResponseListAndJSONArray<HostessInfo>()
		{
			@Override
			public void onRequestSuccess(List<HostessInfo> list, JSONArray array, boolean clear)
			{
				if (array != null)
				{
					jsonArray = array;
				}
				if (!CollectionUtils.isEmpty(list))
				{
					jsonArrayChange = new JSONArray();
					listRoomInfo.clear();
					listRoomInfo.addAll(list);
					adapter = new SortRoomAreaAdapter(SortRoomFirstActivity.this, listRoomInfo, lv_sort_room, jsonArray, jsonArrayChange);
					lv_sort_room.setCanDrag(false);
					lv_sort_room.setAdapter(adapter);
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				if (isLoading)
				{
					ToastUtils.showText(SortRoomFirstActivity.this, "获取分区失败");
				}
			}
		});
	}

	@Override
	public void onBackPressed()
	{
		if (adapter != null && adapter.hasDataChanged())
			showPopSave();
		else
			super.onBackPressed();
	}

	/**
	 * 弹出保存提示框
	 */
	public void showPopSave()
	{
		new VodDialog.Builder(context).dialogEnum(VodDialog.DialogEnum.DOUBLE).btnNegativeText(R.string.cancel).btnNeutralText(R.string.sure).cancelable(true)
				.contentName("修改的开房顺序未保存，确定要返回吗？").onCustomDialogListener(new VodDialog.Builder.OnCustomDialogListener()
				{
					@Override
					public void onOnlyCancelClick()
					{

					}

					@Override
					public void onOkClick()
					{
						finish();
						animRightToLeft();
					}

					@Override
					public void onCancelClick()
					{

					}
				}).build().show();
	}
}
